package com.ehotting.edsta.circuitDesign.gen.arguments.script;

import com.ehotting.edsta.circuitDesign.domain.EsCircuitCodeDataTemplate;
import com.ehotting.edsta.circuitDesign.domain.EsRuleScript;
import com.ehotting.edsta.circuitDesign.gen.ScriptConstants;
import com.ehotting.edsta.circuitDesign.gen.element.bean.ConditionBean;
import com.ehotting.edsta.circuitDesign.gen.element.enums.ConditionTypeEnum;
import com.ehotting.edsta.circuitDesign.gen.element.enums.ScriptTypeEnum;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class ConditionArgumentsScript extends ArgumentsScriptAbstract {

    @Override
    public String invoke(EsRuleScript esScript,  List<String> argumentsList, List<EsCircuitCodeDataTemplate> outputParamList, String functionName) {
        StringBuffer buf = new StringBuffer();
        buf.append(methodStart( functionName, argumentsList));
        buf.append(execute( esScript));
        buf.append(methodEnd());
        return buf.toString();
    }

    protected String execute(EsRuleScript esScript){
        StringBuffer buf = new StringBuffer();
        List<ConditionBean> conditionList = setConditionOrder(esScript);
        buf.append(defineIfBody(conditionList));
        return buf.toString();
    }

    /***
     * 定义子方法体
     * @param conditionList
     * @return
     */
    private String defineIfBody(List<ConditionBean> conditionList){
        StringBuffer buf = new StringBuffer();
        buf.append(ScriptConstants.TAB);
        for(ConditionBean conditionBean : conditionList){
            ConditionTypeEnum cType = ConditionTypeEnum.getObject(conditionBean.getConditionType());
            /**定义 if、else if、else语句**/
            buf.append(cType.getScript());
            if(cType == ConditionTypeEnum.IF || cType == ConditionTypeEnum.ELSE_IF) {
                buf.append("(").append(conditionBean.getConditionScript()).append(")");
            }
            /**定义执行语句**/
            buf.append("{").append(ScriptConstants.LINE);
            String[] lines = conditionBean.getExecute().split(ScriptConstants.LINE);
            for(String line : lines){
                if(line.startsWith("ctx") || line.startsWith("all")){
                    buf.append(line).append(ScriptConstants.LINE);
                }else{
                    String[] ss = line.split("=");
                    //buf.append(ScriptConstants.TAB(2));
                    //buf.append("def ").append(line).append(line.endsWith(";")?"":";").append(ScriptConstants.LINE);
                    buf.append(ScriptConstants.TAB(2));
                    buf.append("ctx."+ss[0]+"="+ss[1]+(line.endsWith(";")?"":";")).append(ScriptConstants.LINE);
                }
                buf.append(ScriptConstants.LINE);
            }
            buf.append(ScriptConstants.TAB).append("}");
        }
        buf.append(ScriptConstants.LINE);
        return buf.toString();
    }

    @Override
    public boolean isTrue(ScriptTypeEnum code){
        return ScriptTypeEnum.CONDITION == code;
    }

    @Override
    public String getExample() {
        StringBuffer buf = new StringBuffer();
        buf.append("输入格式参数:");
        buf.append("输入条件格式，可以使用逻辑符号 \"&&\"、\"||\"分割:");
        buf.append("r1/r2 > 200 && r2.xx >= 200").append(ScriptConstants.LINE);
        buf.append("输入执行语句，多条使用分号分割\";\":");
        buf.append("r1 = 200 ; r2.xx = 200;").append(ScriptConstants.LINE);
        return buf.toString();
    }
}
